package com.guiji.quartz.domain;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.guiji.common.utils.spring.SpringUtils;
import com.guiji.quartz.formatter.ContentFormatter;
import com.guiji.quartz.formatter.DataTemplateFormatter;
import com.guiji.quartz.service.ISpiderNewsService;
import com.guiji.quartz.service.RedisService;
import com.guiji.quartz.task.DataSaveUtil;
import com.guiji.quartz.vo.ExcludeMovie;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.Formatter;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-08-09 11:18
 **/
@TargetUrl(value = "https://www.163.com/*?.html",sourceRegion = "//div[@class='index2016_content']")
@Data
public class WangyiNews implements AfterExtractor {
	private static final Logger log = LoggerFactory.getLogger(WangyiNews.class);
	@ExtractBy(value = "//h1/text()", notNull = true)
	private String title;

	@Formatter(formatter = ContentFormatter.class)
	@ExtractBy(value = "//div[@class='post_body']", notNull = true)
	private String content;

	@ExtractBy("//div[@class='post_body']//p[@class='f_center']//img/@src")
	private List<String> imgList;

	@Formatter(formatter = DataTemplateFormatter.class)
	@ExtractBy("//div[@class='post_info']/text()")
	private Date date;

	@ExtractBy("//div[@class='post_info']/text()")
	private String category;

	@Override
	public void afterProcess(Page page) {
		log.info("wangyiTask-1");
		ISpiderNewsService service = SpringUtils.getBean(ISpiderNewsService.class);
		SpiderNews spider = new SpiderNews();
		spider.setNewsTitle(ChinaNews.filterSpecialCharacter(title));
//		StringBuffer sb = new StringBuffer();
//		content.forEach(s -> sb.append(s+"$$$"));
		spider.setNewsContent(ChinaNews.filterSpecialCharacter(content));
		spider.setContentSize(spider.getNewsContent().length());
		List<String> newImg = delPic(imgList);
		Iterator<String> iterator = newImg.iterator();
		while(iterator.hasNext()){
			if(iterator.next().contains("gif")){
				iterator.remove();
			}
		}
//		spider.setNewsPicJson(JSONUtil.toJsonStr(newImg));
		spider.setNewsUrl(page.getUrl().toString());
		spider.setNewsPublicTime(date);
		spider.setUpdateBy("admin");
		spider.setCreateTime(DateUtil.date());
		spider.setNewsSource("https://news.163.com/");
		ExcludeMovie excludeMovie = SpringUtils.getBean("excludeMovie");
		log.info("wangyiTask-2");
		int k= 0;
		for (String s : excludeMovie.movie) {
			if(title.contains(s)){
				k++;
			}
		}
		if(category.contains("体育")){
			return;
		}else if(category.contains("娱")&&k==0){
			if(title.contains("《")||title.contains("电视剧")||title.contains("电影")){
				spider.setNewsCategory("网易综艺");
			}else {
				spider.setNewsCategory("网易娱乐");
			}
		}else if(category.contains("经")||category.contains("券")){
			spider.setNewsCategory("网易财经");
		}else if(category.contains("科")){
			spider.setNewsCategory("网易科技");
		}else {
			return;
		}
		log.info("wangyiTask-3");
		if(content.length()<5000&&content.replaceAll("\\$|[\\s\\p{Zs}]","").length()>20&&imgList.size()<20){
			RedisService redisService = SpringUtils.getBean(RedisService.class);
			Date ago = DateUtil.offsetDay(new Date(),-3);
			int i = date.compareTo(ago);
			//redis查询是否存在
			boolean ismember = redisService.isMember(SecureUtil.md5(title));
			if(!ismember&&i>0){//如果不存在插入mysql和redis
				List<String> resImg = DataSaveUtil.delPic(newImg);
				spider.setNewsPicJson(JSONUtil.toJsonStr(resImg));
				service.save(spider);
				redisService.addTitleSet(SecureUtil.md5(title), spider.getNewsCategory(), date,title);
			}
		}
		log.info("wangyiTask-4");

	}

	public List<String> delPic(List<String> imgList) {
		List<String> newImg = new ArrayList<>();
		for(String img:imgList){
			String res = img.replaceAll("https.*?=|&.*?jpg","");
			res=res.replaceAll("%3A",":").replaceAll("%2F","/");
			newImg.add(res);
		}
		return newImg;
	}
}
